package com.yikome.blacklist.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yikome.blacklist.service.BlackListService;
import com.google.common.base.Preconditions;
import com.itmuch.lightsecurity.annotation.PreAuthorize;
import com.yikome.blacklist.entity.BlackList;
import com.yikome.blacklist.request.BlackListCreateRequest;
import com.yikome.blacklist.request.BlackListSearchRequest;

import cn.hutool.core.util.StrUtil;

@RestController
@RequestMapping("/blackList")
public class BlackListController {

	@Autowired
	private BlackListService blackListService ; 
	
	/**
	 * 查询所有黑名单
	 */
	@PreAuthorize("hasAnyRoles('admin', 'order', 'shop')")
	@GetMapping("/list")
	public Page<BlackList> findBlackList(BlackListSearchRequest blackListSearchRequest , @PageableDefault(sort = {"o_black_list.id"}, direction = Sort.Direction.DESC)Pageable pageable) {
		
		return blackListService.findBlackList(blackListSearchRequest , pageable);
	}
	
	/**
	 * 删除一个黑名单
	 */
	@PreAuthorize("hasAnyRoles('admin', 'order', 'shop')")
	@DeleteMapping("/{blackListId}")
	public Boolean deleteBlackList(@PathVariable("blackListId") Long blackListId) {
		
		blackListService.deleteBlackList(blackListId);
		
		return Boolean.TRUE;
	}
	
	/**
	 * 新增货修改黑名单那
	 */
	@PreAuthorize("hasAnyRoles('admin', 'order', 'shop')")
	@PostMapping
	public BlackList persistBlackList(@RequestBody BlackListCreateRequest blackListCreateRequest) {
		
		Preconditions.checkArgument(StrUtil.isNotBlank(blackListCreateRequest.getTel()), "参数不全,电话不存在...");
		
		return blackListService.persistBlackList(blackListCreateRequest);
	}
}
